from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from enum import Enum
from . import db

class EventType(Enum):
    TEXT_SEARCH = 'Text-Search'
    METRICS_INQUERY = 'Metrics-Inquery'
    TEXT2SQL = 'TEXT2SQL'
    UNKNOWN = 'Unknown'

class ReviewType(Enum):
    LIKE = 'like'
    DISLIKE = 'dislike'

class Sessions(db.Model):
    __tablename__ = 'sessions'

    session_id = db.Column(db.String(36), primary_key=True)
    user_id = db.Column(db.Integer)
    event_type = db.Column(db.Enum(EventType), nullable=False, default=EventType.UNKNOWN)
    initial_query = db.Column(db.String(500))
    is_active = db.Column(db.Boolean, nullable=False, default=True)
    start_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    end_time = db.Column(db.DateTime)

    # ORM 关联：一个 Session 对应多条 Conversation
    conversations = db.relationship(
        'Conversations',
        backref='session',    # 在 Conversations 上生成 session 属性
        lazy=True
    )

class Conversations(db.Model):
    __tablename__ = 'conversations'
    
    conversation_id = db.Column(db.String(36), primary_key=True)
    session_id = db.Column(db.String(36), db.ForeignKey('sessions.session_id'))
    user_query = db.Column(db.String(500))
    response = db.Column(db.Text)
    key_info = db.Column(db.String(1000))
    review = db.Column(db.Enum(ReviewType))
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)